// =============================================================
// CTask class
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Jul. 25, 2006
//----------------------------------------------------------------------
#include "CTask.h"
//----------------------------------------------------------------------
CTask::CTask(string &name) : CProcess()
{
	this->name = name;
	current_pos = 0;
}
//----------------------------------------------------------------------
CTask::~CTask()
{
}
//----------------------------------------------------------------------
int CTask::read_upstream(const void *buf, size_t size) 
{
	cout << (char *)buf;
}
//----------------------------------------------------------------------
int CTask::read_downstream(const void *buf, size_t size) 
{
}
//----------------------------------------------------------------------
void CTask::before_run() 
{
}
//----------------------------------------------------------------------
int CTask::registerCommonTaskVariables(map<string,string> &variables)
{
	map<string,string>::iterator variable = variables.begin();
	while(variable!=variables.end()) {
		string attr = variable->first;
		string val = variable->second;

		common_task_variables[attr] = val;

		variable++;
	}
}
//----------------------------------------------------------------------
int CTask::registerTaskVariables(map<string,string> &variables)
{
	map<string,string>::iterator variable = variables.begin();
	while(variable!=variables.end()) {
		string attr = variable->first;
		string val = variable->second;

		task_variables[attr] = val;

		variable++;                             
	}
}
//----------------------------------------------------------------------
int CTask::registerAction(CAction &action)
{
	actions.push_back(action);

	current_pos = actions.size();
	return current_pos;
}
//----------------------------------------------------------------------
string &CTask::getName()
{
	return name;
}
//----------------------------------------------------------------------
string &CTask::getParameter(char *cattr)
{
	string attr = cattr;
	return getParameter(attr);
}
//----------------------------------------------------------------------
string &CTask::getParameter(string &attr)
{
	string common_task_variable = common_task_variables[attr];
	string task_variable = task_variables[attr];

	if(task_variable!="") return task_variables[attr];
	else return common_task_variables[attr];
}
//----------------------------------------------------------------------
int CTask::hasMoreActions()
{
	return current_pos;
}
//----------------------------------------------------------------------
CAction &CTask::getNextAction()
{
	int i = actions.size() - current_pos--;
	return actions[i];
}
//----------------------------------------------------------------------
void CTask::run()
{
	for(int i=0;i<actions.size();i++) {
		cout << endl;
		cout << "[action]" << actions[i].getName() << endl;
		cout << "--------------------------------------------" << endl;

		actions[i].perform();

		cout << "--------------------------------------------" << endl;
		cout << endl;
	}
}
//----------------------------------------------------------------------
